package cn.gok.homework;




/*1 创建类 School类：name,address,  birthYear成立的年份：int （2002；2010）
        实现序列化接口，void writeToFile();//采用对象流写入将两个school对象写入到 1.dat中，
        void readFromFile();//从1.dat中读取数据到对象，并显示出来



*/

import cn.gok.homework.timu2.Student;

import java.io.*;
import java.util.Comparator;
import java.util.TreeSet;

class School implements Serializable {
    private String name;
    private String address;
    private int birthYear;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }


    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }
}

public class homework5 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //writeToFile();
        //readFromFile();

        cn.gok.homework.timu2.Student s1 = new cn.gok.homework.timu2.Student(19,"张三1",20);
        cn.gok.homework.timu2.Student s2 = new cn.gok.homework.timu2.Student(20,"张三2",20);
        cn.gok.homework.timu2.Student s3 = new cn.gok.homework.timu2.Student(21,"张三3",20);
        cn.gok.homework.timu2.Student s4 = new cn.gok.homework.timu2.Student(18,"张三4",20);
        cn.gok.homework.timu2.Student s5 = new cn.gok.homework.timu2.Student(19,"张三5",18);
        TreeSet<cn.gok.homework.timu2.Student> treeSet = new TreeSet<>();

        treeSet.add(s2);
        treeSet.add(s1);
        treeSet.add(s3);
        treeSet.add(s4);
        treeSet.add(s5);
    }


    public static void writeToFile() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day5//1.dat"));
        //创建两个school对象
        School s1 = new School("泉大", "jinjiang", 1986);
        School s2 = new School("泉轻", "jinjiang", 2009);

        oos.writeObject(s1);
        oos.writeObject(s2);
        oos.close();
        System.out.println("写入成功");


    }

    //读取文件
    public static void readFromFile() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day5//1.dat"));
        //不断的读取文件，直到文件末尾
        try {


            while (true) {
                Object obj = ois.readObject();//作为一般对象读取
                System.out.println(obj);//读一个文件，输出一个文件
            }
        } catch (EOFException e) {//通过捕获文件末尾异常，跳出死循环
            System.out.println("文件读取结束");
        }


    }

    class Student implements Comparable<cn.gok.homework.timu2.Student>{
        private int sid;
        private String name;
        private int age;

        public Student(int sid, String name, int age) {
            this.sid = sid;
            this.name = name;
            this.age = age;
        }

        public int getSid() {
            return sid;
        }

        public void setSid(int sid) {
            this.sid = sid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }



        @Override
        public int compareTo(cn.gok.homework.timu2.Student o) {

            System.out.println(this.toString() +" 比Comparable较 "+  o.toString());
            if (this.sid < o.getSid()) {
                return -1;
            } else if (this.sid == o.getSid()) {
                if(this.age<o.getAge()){
                    return -1;
                }else {
                    return 1;
                }
            }
            return 1;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "sid=" + sid +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

//    （二）Comparator方法

        public class StuComparetor implements Comparator<cn.gok.homework.timu2.Student> {
            @Override
            public int compare(cn.gok.homework.timu2.Student o1, cn.gok.homework.timu2.Student o2) {

                System.out.println(o1.toString() +" 比Comparator较 "+  o2.toString());

                if (o1.getSid() < o2.getSid()) {
                    return -1;
                } else if (o1.getSid() == o2.getSid()) {
                    if(o1.getAge()<o2.getAge()){
                        return -1;
                    }else {
                        return 1;
                    }
                }
                return 1;
            }
        }
    }



}



